package com.example.yichuangyun.mvvm.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.databinding.ItemDetailsContactWayBinding;
import com.example.yichuangyun.model.contacts.ContactWayModel;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.UiUtils;

import java.util.ArrayList;
import java.util.List;

public class ContactWayAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final Context context;
    private List<ContactWayModel> contactWayData;
    private OnCallListener onCallListener;

    public ContactWayAdapter(Context context, List<ContactWayModel> contactWayData) {
        this.context = context;
        this.contactWayData = contactWayData;
    }

    public void onRefresh(List<ContactWayModel> mList) {
        if (mList != null) {
            if (contactWayData == null) {
                contactWayData = new ArrayList<>();
            }
            this.contactWayData.clear();
            this.contactWayData.addAll(mList);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemDetailsContactWayBinding itemDetailsContactWayBinding = ItemDetailsContactWayBinding.inflate(LayoutInflater.from(context), parent, false);
        return new ContactWayViewHolder(itemDetailsContactWayBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        if (holder instanceof ContactWayAdapter.ContactWayViewHolder) {
            ContactWayViewHolder contactWayViewHolder = (ContactWayViewHolder) holder;
            contactWayViewHolder.itemView.setPadding(0, 0, 0, 0);

            ContactWayModel model = contactWayData.get(position);
            contactWayViewHolder.binding.tvContactWay.setText(UiUtils.getContactTypeName(model.getType()));
            if (position > 0 && model.getType() == 1 && model.getType() == contactWayData.get(position - 1).getType()) {
                contactWayViewHolder.binding.tvContactWay.setVisibility(View.GONE);
            } else {
                contactWayViewHolder.binding.tvContactWay.setVisibility(View.VISIBLE);
            }
            if (position != contactWayData.size() - 1 && model.getType() == 1 && model.getType() == contactWayData.get(position + 1).getType()) {
                contactWayViewHolder.binding.line.setVisibility(View.GONE);
            } else {
                contactWayViewHolder.binding.line.setVisibility(View.VISIBLE);
            }
            contactWayViewHolder.binding.tvContactInfo.setText(CommonUtil.checkText(model.getInfo()));
            if (model.getType() == 1 || model.getType() == 5) {
                contactWayViewHolder.binding.tvContactInfo.setTextColor(0xFF426FFF);
            } else {
                contactWayViewHolder.binding.tvContactInfo.setTextColor(0xFF3B3A40);
            }
            contactWayViewHolder.binding.tvContactInfo.setOnClickListener(view -> {
                if (model.getType() == 1 || model.getType() == 5) {
                    if (onCallListener != null && !TextUtils.isEmpty(model.getInfo())) {
                        onCallListener.callListener(model.getInfo());
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return contactWayData.size();
    }

    static class ContactWayViewHolder extends RecyclerView.ViewHolder {

        ItemDetailsContactWayBinding binding;

        public ContactWayViewHolder(ItemDetailsContactWayBinding itemDetailsContactWayBinding) {
            super(itemDetailsContactWayBinding.getRoot());
            binding = itemDetailsContactWayBinding;
        }
    }

    public interface OnCallListener {
        void callListener(String number);
    }

    public void setOnCallListener(OnCallListener onCallListener) {
        this.onCallListener = onCallListener;
    }
}
